﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Maps.Controls;

namespace FastestWays.Helpers
{
    public class MyMapExtensions
    {
        #region public attached List<MapElement> MapElementsCollection
        /// <summary>
        /// Gets the value of the MapElementsCollection attached property for a specified Map.
        /// </summary>
        /// <param name="element">The Map from which the property value is read.</param>
        /// <returns>The MapElementsCollection property value for the Map.</returns>
        public static List<MapElement> GetMapElementsCollection(Map element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            return element.GetValue(MapElementsCollectionProperty) as List<MapElement>;
        }

        /// <summary>
        /// Sets the value of the MapElementsCollection attached property to a specified Map.
        /// </summary>
        /// <param name="element">The Map to which the attached property is written.</param>
        /// <param name="value">The needed MapElementsCollection value.</param>
        public static void SetMapElementsCollection(Map element, List<MapElement> value)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            element.SetValue(MapElementsCollectionProperty, value);
        }

        /// <summary>
        /// Identifies the MapElementsCollection dependency property.
        /// </summary>
        public static readonly DependencyProperty MapElementsCollectionProperty =
            DependencyProperty.RegisterAttached(
                "MapElementsCollection",
                typeof(List<MapElement>),
                typeof(MyMapExtensions),
                new PropertyMetadata(null, OnMapElementsCollectionPropertyChanged));

        /// <summary>
        /// MapElementsCollectionProperty property changed handler.
        /// </summary>
        /// <param name="d">MyMapExtensions that changed its MapElementsCollection.</param>
        /// <param name="e">Event arguments.</param>
        private static void OnMapElementsCollectionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Map source = d as Map;
            List<MapElement> value = e.NewValue as List<MapElement>;

            var dispatcher = Deployment.Current.Dispatcher;
            if (!dispatcher.CheckAccess())
            {
                dispatcher.BeginInvoke(() => { ReAddMapElementsToCollection(source, value); });
            }
            else
            {
                ReAddMapElementsToCollection(source, value);
            }
            
        }

        private static void ReAddMapElementsToCollection(Map source, List<MapElement> value)
        {
            if (source != null && value != null)
            {
                source.MapElements.Clear();
                foreach (var mapElement in value)
                {
                    source.MapElements.Add(mapElement);
                }
            }
        }

        #endregion public attached List<MapElement> MapElementsCollection

    }
}
